package com.bhs.sansonglogistics.ui.consumptionCoupon;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.event.CouponRefresh;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;
import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;

import org.greenrobot.eventbus.EventBus;

import java.math.BigDecimal;
import java.util.GregorianCalendar;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 发券
 */
public class IssueCouponsActivity extends BaseActivity implements View.OnClickListener, NetCallBack, TextWatcher {

    private EditText mEtSatisfyMoney;
    private EditText mEtSubtractMoney;
    private EditText mEtNumber;
    private TextView mTvTime;
    private Button mBtnSubmit;
    private TextView mTvIssueAmount;

    @Override
    public int initLayout() {
        return R.layout.activity_issue_coupons;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("发券");
        mEtSatisfyMoney = findViewById(R.id.et_satisfy_money);
        mEtSubtractMoney = findViewById(R.id.et_subtract_money);
        mEtNumber = findViewById(R.id.et_number);
        findViewById(R.id.ll_time).setOnClickListener(this);
        mTvTime = findViewById(R.id.tv_time);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mEtNumber.addTextChangedListener(this);
        mEtSatisfyMoney.addTextChangedListener(this);
        mEtSubtractMoney.addTextChangedListener(this);
        mTvIssueAmount = findViewById(R.id.tv_issue_amount);
        mBtnSubmit.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ll_time) {
            new CardDatePickerDialog.Builder(mActivity)
                    .setTitle("选择有效时间")
                    .showBackNow(false)
                    .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY)
                    .setMinTime(System.currentTimeMillis())
                    .setThemeColor(Color.parseColor("#485EF4"))
                    .setWrapSelectorWheel(false)
                    .setOnChoose("确定", aLong -> {
                        mTvTime.setText(DateUtils.getYearMonthDay(aLong));
                        verify();
                        return null;
                    }).build().show();
        } else if (view.getId() == R.id.btn_submit) {
            submit();
        }
    }

    private void submit() {
        if (Integer.parseInt(mEtSatisfyMoney.getText().toString()) > 99999) {
            ToastUtil.show("满足金额必须小于100000");
            return;
        }
        if (Integer.parseInt(mEtSubtractMoney.getText().toString()) > Integer.parseInt(mEtSatisfyMoney.getText().toString())) {
            ToastUtil.show("优惠金额不能大于满足金额");
            return;
        }
        loadingPopupShow();
        Map<String, String> map = new ArrayMap<>();
        map.put("coupon_num", mEtNumber.getText().toString());
        map.put("date_from", DateUtils.getYearMonthDay(System.currentTimeMillis()));
        map.put("date_end", mTvTime.getText().toString());
        map.put("coupon_min_price", mEtSatisfyMoney.getText().toString());
        map.put("coupon_price", mEtSubtractMoney.getText().toString());
        networkRequest(netApi.distribute(map), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        loadingPopupDismiss();
        if (bean.getCode().equals("1")) {
            ToastUtil.show("发券成功");
            EventBus.getDefault().post(new CouponRefresh(1));
            finish();
        } else {
            ToastUtil.show(bean.getMsg());
        }

    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        verify();
    }

    private void verify() {
        boolean verify = !TextUtils.isEmpty(mEtSubtractMoney.getText())
                && !TextUtils.isEmpty(mEtSatisfyMoney.getText())
                && !TextUtils.isEmpty(mEtNumber.getText());
        if (verify) {
            BigDecimal bigDecimal = new BigDecimal(mEtSubtractMoney.getText().toString());
            bigDecimal = bigDecimal.multiply(new BigDecimal(mEtNumber.getText().toString()));
            mTvIssueAmount.setText(String.format("发券金额:%s元", bigDecimal));
        }
        if (TextUtils.isEmpty(mTvTime.getText())) {
            verify = false;
        }

        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }
}